<template>
	<view class=" ">
		<view class="">
			<view>
				
				 
				<view class="f-d   p_40 a-c bb">
					<view class="f32 " style="width: 180rpx;">
						<text class="f32 ">联系人</text>
					</view>
					<input class="f28 in" type="text" placeholder="请输入姓名" v-model="name" />
				</view>

				<view class="f-d   p_40 a-c bb">
					<view class="f32 " style="width: 180rpx;">
						<text class="f32 ">手机号</text>
					</view>
					<input class="f28 in" type="text" placeholder="请输入手机号码" v-model="tel" />
				</view>
				<view class="f-d   p_40 a-c bb">
					<view class="f-d " style="width: 530rpx;">
						<view class="" style="width: 180rpx;">
							<text class="f28 ">选择位置</text>
						</view>
						<textarea class="f28 in" style="width: 65%;" type="text" auto-height="" placeholder="请在地图上选择位置"
							v-model="shop_addr" />
					</view>
					<view class="">
						<text @click="on_dingwen()" class="button_bg  q_y f28 pr2 pl2">选择位置</text>
					</view>
				</view>
				<view class="f-d  p_40 a-c bb">
					<view class="f32" style="width: 180rpx;">
						<text class="f32  ">门牌号</text>
					</view>
					<input class="f28 in" type="text" placeholder="楼号,门牌等信息" v-model="addr_details" />
				</view>
				<view class="flex j-b  p_40 a-c bb">
					<view class="f32 " style="width: 180rpx;">
						<text class="f32  ">设为默认</text>
					</view>
					<view @click="on_select()" class="flex a-c  switch_sub mr2" :class="select==1 ? '' : 'j-f'">
						<view class="switch" :class="select==2?'buttonswitchSelected':'buttonswitch'">
						</view>
					</view>
				</view>
			</view>
			<view class="flex j-c a-c  pt3">
				<button class="button_bg flex j-c a-c"
					style="margin: 20upx;width: 600rpx;border-radius: 45rpx;height: 40px;" @tap="on_post">
					<text class=" p20">保存</text>
				</button>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		components: {},
		data() {
			return {

				idtushow: false,
				isYasuo: true,
				imageUrl: '',
				show: false,
				url: '',
				isdingwei: true,

				list: [],
				cata_name: '请选择',
				name: '',
				tel: '',
				shop_addr: '',
				is_tupian: 0,
				select: 2,
				selectLocatin: {
					location: {}
				},

				rendering: false,
				addr_details: '',
				id: 0

			}
		},


		onLoad(on) {
			this.token = uni.getStorageSync('token');
			var that = this;
			if (on.id > 0) {
				this.id = on.id;
				this.init();
			}

		},
		methods: {
			on_select() {
				var select = 2
				if (this.select == 2) {
					select = 1
				}
				this.select = select;
			},
			on_dingwen() {
				this.token = uni.getStorageSync('token');
				if (!this.token) {
					this.is_log();
					return
				}
				let that = this;
				uni.chooseLocation({
					success: function(res) {

						that.shop_addr = res.address;
						that.shop_lat = res.latitude;
						that.shop_lng = res.longitude;
					}
				});
			},
			confirm(val) {
				this.idtushow = false;
				this.shop_lat = val.location.lat
				this.shop_lng = val.location.lng
				this.shop_addr = val.address
			},
			isPhone() {
				let mPattern = /^1[23456789]\d{9}$/;
				return mPattern.test(this.shop_tel);
			},



			on_post() {
				this.token = uni.getStorageSync('token');
				if (!this.token) {
					this.is_log();
					return
				}
				if (!this.name) {
					uni.showToast({
						title: "请输入姓名",
						icon: 'none'
					})
					return
				}
				if (!this.tel) {
					uni.showToast({
						title: "请输入联系电话",
						icon: 'none'
					})
					return
				}
				if (!this.shop_lng) {
					uni.showToast({
						title: "请选择地址",
						icon: 'none'
					})
					return
				}
				if (!this.addr_details) {
					uni.showToast({
						title: "请输入门牌号",
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					title: '正在提交中'
				})

				this.add_shop();

			},
			add_shop() {


				// var shop_img = this.shop_img.replace(this.img_url, "")
				var that = this;
				let requestParams = {
					user_id: uni.getStorageSync('user_id'),
					token: uni.getStorageSync('token'),
					name: this.name,
					tel: this.tel,
					addr: this.shop_addr,
					addr_details: this.addr_details,
					id: this.id,
					shop_lng: this.shop_lng,
					shop_lat: this.shop_lat,
					select: this.select
				};
				this.Http.post('/banmi.php/User/UserAddAddr', requestParams).then(ret => {
					uni.hideLoading()
					uni.showToast({
						title: ret.msg,
						icon: 'none'
					})
					if (ret.status == 200) {
						setTimeout(function() {
							uni.$emit('banmi_addr', {
								title: 1,
							});
							uni.navigateBack()
						}, 500);

						return
					}

				})
			},

			init() {
				var that = this;
				let requestParams = {
					user_id: uni.getStorageSync('user_id'),
					token: uni.getStorageSync('token'),
					id: this.id,
				};
				this.Http.post('/banmi.php/User/UserAddrFind', requestParams).then(ret => {
					if (ret.status == 200) {
						var shop_find = ret.data.find;
						this.img_url = ret.url
						if (shop_find) {
							that.title = '编辑地址';
							uni.setNavigationBarTitle({
								title: that.title
							})
							that.name = shop_find.name;
							that.shop_addr = shop_find.addr;
							that.tel = shop_find.tel;
							that.addr_details = shop_find.addr_details;
							that.shop_lng = shop_find.lng;
							that.shop_lat = shop_find.lat;
							that.select = shop_find.select;






						}
					}
				})
			},




		}
	}
</script>

<style>









</style>